// miniprogram/pages/rumor-background-tester/rumor-background-tester.js
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {

  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },

  queryUserByOpenid: function(){
    wx.request({
      url : "http://localhost:8081/queryUser",
      method: "POST",
      data: {
        open_id: "open_id"
      },
      header: {
        "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function (res) {
        console.log(res.data);
        wx.showToast({
          title: '请求成功！',
          icon: 'success',
          duration: 2000
        })
      },
    })
  },

  login: function(){
    wx.login({
      success: res => {
        // 发送 res.code 到后台换取 openId, sessionKey, unionId
        if (res.code) {
          wx.request({
            url: 'http://localhost:8081/login',
            method: "POST",
            data: {
              code: res.code
            },
            header: {
              'content-type': 'application/x-www-form-urlencoded'
            },
            success(res) {
              console.log(res.data);
              if (res.data.username != "" || res.data.username != null) {
                // 登录成功
                wx.setStorageSync("username", res.data.username);//将用户id保存到缓存中
              } else {
                // 登录失败
                // TODO 跳转到错误页面，要求用户重试
                return false;
              }
            }
          })
        } else {
          console.log('获取用户登录态失败！' + res.errMsg)
        }
      }
    })
  },
  
  addSCUTStudentUser: function(){
    wx.request({
      url : "http://localhost:8081/addSCUTStudentUser",
      method: "POST",
      data: {
        username: "user-44D833xzpdSB",
        nickname: "ssutest4321",
        sex: "男",
        user_type: 1,
        grade: "大三",
        college: "软2342件学院",
        major: "软件2342工程",
        phone: "3478412"
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log(res.data);
        wx.showToast({
          title: '请求成功！',
          icon: 'success',
          duration: 2000
        })
      },
    })
  },

  addSCUTGraduateUser: function(){
    wx.request({
      url : "http://localhost:8081/addSCUTGraduateUser",
      method: "POST",
      data: {
        username: "ss4333u1",
        nickname: "ssutest4321",
        sex: "男",
        user_type: 1,
        graduation_year: "2018",
        college: "软2342件学院",
        major: "软件2342工程",
        phone: "3478412"
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log(res.data);
        wx.showToast({
          title: '请求成功！',
          icon: 'success',
          duration: 2000
        })
      },
    })
  },

  addSCUTStaffUser: function(){
    wx.request({
      url : "http://localhost:8081/addSCUTStaffUser",
      method: "POST",
      data: {
        username: "41dgfdsadfasf",
        nickname: "ssutest4321",
        sex: "男",
        user_type: "3",
        phone: "3478412"
      },
      header: {
        "Content-Type": "application/json"
      },
      success: function (res) {
        console.log(res.data);
        wx.showToast({
          title: '请求成功！',
          icon: 'success',
          duration: 2000
        })
      },
    })
  }

  // imgUpload: function(){
  //   wx.chooseImage({
  //     success(res) {
  //       const tempFilePaths = res.tempFilePaths
  //       wx.uploadFile({
  //         url: 'http://localhost:8080/rumorContribute', //仅为示例，非真实的接口地址
  //         filePath: tempFilePaths[0],
  //         name: 'file',
  //         formData: {
  //           description: '图片'
  //         },
  //         success(res) {
  //           console.log(res.data)
  //         }
  //       })
  //     }
  //   })
  // },
})